﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Xml;


#if NET_1_1
    using ICSharpCode.SharpZipLib.GZip;
#else
using System.IO.Compression;
using System.Net.Cache;
using System.Net.Security;
using System.Security.Authentication;
#endif

public partial class orderQuery1 : System.Web.UI.Page
{
    public String srcMsg = null;
    public String signMsg = null;
    public String verifySrc = null;
    public bool verifyResult = false;

    public String serverUrl;
    public String key;
    public String version;
    public String merchantId;
    public String signType;
    public String orderNo;
    public String orderDatetime;
    public String queryDatetime;

    public String querySignSrcMsg;
    public String querySignMsg;

    protected void Page_Load(object sender, EventArgs e)
    {
        //填充交易数据
        serverUrl = Request.Params["serverUrl"];  //submit server url 
        key = Request.Params["key"];   //md5 key
        version = Request.Params["version"]; //version
        merchantId = Request.Params["merchantId"];//merchantId
        signType = Request.Params["signType"];
        orderNo = Request.Params["orderNo"];
        orderDatetime = Request.Params["orderDatetime"];
        queryDatetime = Request.Params["queryDatetime"];


        //生成signMsg
        //1、首先组签名原串 querySignSrcMsg
        StringBuilder bufSignSrc = new StringBuilder();
        appendSignPara(bufSignSrc, "merchantId", merchantId);
        appendSignPara(bufSignSrc, "version", version);
        appendSignPara(bufSignSrc, "signType", signType);
        appendSignPara(bufSignSrc, "orderNo", orderNo);
        appendSignPara(bufSignSrc, "orderDatetime", orderDatetime);
        appendSignPara(bufSignSrc, "queryDatetime", queryDatetime);
        appendLastSignPara(bufSignSrc, "key", key);

        querySignSrcMsg = bufSignSrc.ToString();
        //2、再对签名原串进行MD5摘要
        querySignMsg = FormsAuthentication.HashPasswordForStoringInConfigFile(querySignSrcMsg, "MD5");

    }




    //---------------------------------------以下代码请勿更动------------------------------------------------------------

    private bool isEmpty(String src)
    {
        if (null == src || "".Equals(src) || "-1".Equals(src))
        {
            return true;
        }
        return false;
    }

    private void appendSignPara(System.Text.StringBuilder buf, String key, String value)
    {
        if (!isEmpty(value))
        {
            buf.Append(key).Append('=').Append(value).Append('&');
        }
    }

    private void appendLastSignPara(System.Text.StringBuilder buf, String key,
            String value)
    {
        if (!isEmpty(value))
        {
            buf.Append(key).Append('=').Append(value);
        }
    }
}
